package com.itbour.service.yjx.trade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.google.gson.JsonObject;
import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.common.util.StringUtils;
import com.itbour.dao.yjx.trade.YjxCustAniDao;
import com.itbour.dto.tg.DisUserdocDto;
import com.itbour.dto.video.VideoDesignDto;
import com.itbour.dto.yjx.trade.YjxCustAniDto;
import com.itbour.dto.yjx.trade.YjxVideoUserDocDto;
import com.itbour.dto.yjx.user.YjscItemTotal;
import com.itbour.service.yjx.trade.YjxCustAniService;

public class YjxCustAniServiceImpl implements YjxCustAniService {
	
	private YjxCustAniDao yjxCustAniDao;

	public Page getCustAniInfo(Map<String, Object> param, Integer pageNo,
			Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxCustAniDao.getCustAniCnt(param);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<YjxCustAniDto> data = yjxCustAniDao.getCustAniInfo(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}

	
	// 插入 自定义动画信息
	public boolean insertCustAni(YjxCustAniDto yjxCustAniDto) {
		try {
			Map<String, Object> parm =new HashMap<String, Object>();
			if (yjxCustAniDto.getUser_id() == null){
				return false;
			}
			if (yjxCustAniDto.getAni_name() == null){
				return false;
			}
			if (yjxCustAniDto.getDuration() == null){
				return false;
			}
			parm.put("tmpl_json",yjxCustAniDto.getTmpl_json());
			parm.put("tmpl_set",yjxCustAniDto.getTmpl_set());
			parm.put("duration",yjxCustAniDto.getDuration());
			parm.put("ani_name",yjxCustAniDto.getAni_name());
			parm.put("user_id",yjxCustAniDto.getUser_id());
			parm.put("url",yjxCustAniDto.getUrl());
			parm.put("thumb_v",yjxCustAniDto.getThumb_v());
			parm.put("thumbs",yjxCustAniDto.getThumbs());
			parm.put("size_id",90);
			parm.put("update_by",yjxCustAniDto.getUser_id());
			yjxCustAniDao.insertCustAniInfo(parm);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public YjxCustAniDao getYjxCustAniDao() {
		return yjxCustAniDao;
	}

	public void setYjxCustAniDao(YjxCustAniDao yjxCustAniDao) {
		this.yjxCustAniDao = yjxCustAniDao;
	}


	public boolean delCustAni(Map<String, Object> parm) {
		try {
			yjxCustAniDao.delCustAniInfo(parm);
			return true;
		} catch (Exception e) {
			return false;
		}
	}


	public List<YjxCustAniDto> getOneCustInfo(Map<String, Object> parm) {
		return yjxCustAniDao.getOneCustAniInfo(parm);
	}

	
	public Page getVideoUserDoc(Map<String, Object> param, Integer pageNo,
			Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxCustAniDao.getVideoUserDocCnt(param);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<YjxVideoUserDocDto> data = yjxCustAniDao.getVideoUserDocInfo(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}


	public List<YjxVideoUserDocDto> getOneVideoUserDoc(Map<String, Object> parm) {
		List<YjxVideoUserDocDto> list = new ArrayList<YjxVideoUserDocDto>();
		List<YjxVideoUserDocDto> list2 = new ArrayList<YjxVideoUserDocDto>();
		List<YjxCustAniDto> custList = new ArrayList<YjxCustAniDto>();
		list = yjxCustAniDao.getVideoTempData(parm);
		
		if (list.size()<=0){
			return list2;
		}
		String anids = null;
		YjxVideoUserDocDto yjxVideoUserDocDto = list.get(0);
		if (yjxVideoUserDocDto.getCustAniIds()!=null){
			anids = yjxVideoUserDocDto.getCustAniIds();
			String b = anids.replaceAll("[\\[\\]]", "");// 去掉中括号
			if ((!b.equals(null)) && (!b.equals(""))){
				String ids = StringUtils.listToString(StringUtils.stringToList(b));
				JSONObject json = new JSONObject();
				if (ids!=null){
					parm.put("ids", ids);
					custList = yjxCustAniDao.getOneCustAniInfo(parm);
					for (YjxCustAniDto yjxCustAniDto : custList) {
						json.put(yjxCustAniDto.getAni_id().toString(), yjxCustAniDto.getTmpl_json());
					}
				}
				yjxVideoUserDocDto.setTmpl_json(json.toString());
			}
		}
		
		list2.add(yjxVideoUserDocDto);
		return list2;
	}


	// 获取 用户设计作品
	public Page getDesignUserDoc(Map<String, Object> parm, Integer pageNo,
			Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			List<DisUserdocDto> data = null;
			int totalSize = 0;
			if (parm.get("mobile")!=null){ // 如果手机号不为空 就去查用户ID
				Long userId = yjxCustAniDao.getItbUserId(parm);
				if (userId != null){
					parm.put("user_id",userId);
				} else {
					return new JspPage(skipResults, totalSize, pageSize, data);
				}
			}
			if (parm.get("doc_name")!=null){
				parm.put("pageSize", 1);
				parm.put("skipResults", 0);
				data = yjxCustAniDao.getDesignUserDocInfo(parm);
				totalSize = data.size();
			} else {
				totalSize = yjxCustAniDao.getDesignUserDocCnt(parm);
				while(totalSize >0 && totalSize <= skipResults){
					skipResults= skipResults - pageSize;
					pageNo = pageNo -1;
				}
				parm.put("pageSize", pageSize);
				parm.put("skipResults", skipResults);
				data = yjxCustAniDao.getDesignUserDocInfo(parm);
			}
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}


	public List<DisUserdocDto> getOneDesignUserDoc(Map<String, Object> parm) {
		return yjxCustAniDao.getDesignTempData(parm);
	}


	// 获取视频 模板列表数据
	public Page getVideoTmpl(Map<String, Object> parm, Integer pageNo,
			Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxCustAniDao.getVideoTmplCnt(parm);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			parm.put("pageSize", pageSize);
			parm.put("skipResults", skipResults);
			List<VideoDesignDto> data = yjxCustAniDao.getVideoTmplInfo(parm);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}


	// 获取单个视频data
	public List<VideoDesignDto> getOneVideoDataInfo(Map<String, Object> parm) {
		List<VideoDesignDto> list = new ArrayList<VideoDesignDto>();
		List<VideoDesignDto> list2 = new ArrayList<VideoDesignDto>();
		List<YjxCustAniDto> custList = new ArrayList<YjxCustAniDto>();
		list = yjxCustAniDao.getVideoTmplData(parm);
		String anids = null;
		VideoDesignDto videoDesignDto = list.get(0);
		if (videoDesignDto.getCustAniIds()!=null){
			anids = videoDesignDto.getCustAniIds();
			String b = anids.replaceAll("[\\[\\]]", "");// 去掉中括号
			if ((!b.equals(null)) && (!b.equals(""))){
				String ids = StringUtils.listToString(StringUtils.stringToList(b));
				JSONObject json = new JSONObject();
				if (ids!=null){
					parm.put("ids", ids);
					custList = yjxCustAniDao.getOneCustAniInfo(parm);
					for (YjxCustAniDto yjxCustAniDto : custList) {
						json.put(yjxCustAniDto.getAni_id().toString(), yjxCustAniDto.getTmpl_json());
					}
				}
				videoDesignDto.setAni_json(json.toString());
			}
		}
		list2.add(videoDesignDto);
		return list2;
	}


	/**	获取VIP用户的
	 */
	public Page getVipDesignUserDoc(Map<String, Object> parm, Integer pageNo,
			Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			
			Long total = yjxCustAniDao.getMaxUserDocId(parm);// 两百万
			long val = 100000;
			int len = (int) Math.ceil(Float.valueOf(total)/Float.valueOf(val));
			
			int cont = 0;
			Long a = 1l;
			Long b = val;
			for(int i =1;i<=len;i++){// 循环查询
				parm.put("betS", a);
				parm.put("betE", b);
				cont += yjxCustAniDao.getDesignVipUserDocCnt(parm);
				
				a = a+val;
				b = b+val;
				if (b>total){// 如果最大
					b = total;
				}
			}
			
			
			// 暂时给固定值
			int totalSize = cont;
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			parm.put("pageSize", pageSize);
			parm.put("skipResults", skipResults);
			List<DisUserdocDto> data = yjxCustAniDao.getDesignVipUserDocInfo(parm);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}


	public Page getCustVideoDoc(Map<String, Object> parm, Integer pageNo,
			Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxCustAniDao.getCustVideoDocCnt(parm);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			parm.put("pageSize", pageSize);
			parm.put("skipResults", skipResults);
			List<YjxVideoUserDocDto> data = yjxCustAniDao.getCustVideoDoc(parm);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}


	// 获取一键秀自由创建数据
	public List<YjxVideoUserDocDto> getOneVideoDocData(Map<String, Object> parm) {
		List<YjxVideoUserDocDto> list = new ArrayList<YjxVideoUserDocDto>();
		Object st = yjxCustAniDao.getCustVideoDocData(parm);// 获取
		JSONObject jsonObject = JSONObject.fromObject(st);
		if (jsonObject.isNullObject()){// 如果是空对象
			return list;
		}
		
		JSONArray jsonArry = jsonObject.getJSONArray("transitions");
		Set<String> ids = new HashSet<String>();
		if (jsonArry.size()>0){
			for(int i=0;i<jsonArry.size();i++){
				JSONObject tmp = jsonArry.getJSONObject(i);
				String dbId = tmp.get("dbId").toString();
				ids.add(dbId);
			}
		}
		
		List<String> result = new ArrayList<String>(ids);
		YjxVideoUserDocDto yDocDto = new YjxVideoUserDocDto();
		if (result.size()>0){
			parm.put("ids", StringUtils.listToString(result));
			List<YjxCustAniDto> antList = yjxCustAniDao.getOneCustAniInfo(parm);
			JSONObject jsonAnt = new JSONObject();
			if (antList.size()>0){
				for (YjxCustAniDto yjxCustAniDto : antList) {
					jsonAnt.put(yjxCustAniDto.getAni_id(), yjxCustAniDto.getTmpl_json());
				}
				yDocDto.setTmpl_json(String.valueOf(jsonAnt));
			}
		}
		
		
		yDocDto.setV_data(st.toString());
		list.add(yDocDto);
		return list;
	}
	
	
}
